package com.huitone.gddw.service.impl.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.dao.business.IWebSmRecDao;
import com.huitone.gddw.dao.entity.WebSmRec;
import com.huitone.gddw.service.business.IWebSmRecService;
import com.huitone.gddw.service.dto.business.UploadWebSmDTO;
import com.huitone.gddw.utils.DateTimeUtils;
import com.huitone.gddw.utils.SpringContextUtil;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 门户发送短信记录 接口实现类
 * @author wuluodan
 *
 */
@Service
public class WebSmRecService implements IWebSmRecService {

	@Autowired
	private IWebSmRecDao webSmRecDao;
	
	@Autowired
	private SpringContextUtil springContextUtil;
	
	@SuppressWarnings("static-access")
	@Override
	public int saveBatch(List<WebSmRec> list) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", list.get(0).getSvrNode() != null ? list.get(0).getSvrNode().substring(1) : null);
		
		List<WebSmRec> li = new ArrayList<WebSmRec>();
		li.addAll(list);
		final IWebSmRecService webSmRecServiceI = (IWebSmRecService)springContextUtil.getBean("webSmRecService");
		while (list.size() != 0) {
			int size = 1000;
			int index = 0;
			if (list.size() < 1000) {
				size = list.size();
			}
			final List<WebSmRec> temp = new ArrayList<WebSmRec>();
			while (index < size) {
				temp.add(list.get(0));
				list.remove(0);
				index++;
			}

			new Thread() {
				public void run() {
					webSmRecServiceI.insertbatch(temp);
				}
			}.start();
		}
		list.addAll(li);
		return 0;
	}
	
	@Override
	public int insertbatch(List<WebSmRec> list) {
		return webSmRecDao.saveBatch(list);
	}

	@Override
	public List<WebSmRec> getByFileId(String fileId) {
		return webSmRecDao.getByFileId(fileId);
	}

	@Override
	public void updateBatch(List<WebSmRec> list) {
		if (!list.isEmpty() && list.size() > 0) {
			webSmRecDao.updateBatch(list);
		}
	}

	@Override
	public void delAllByFileId(String fileId) {
		webSmRecDao.delAllByFileId(fileId);
	}

	@Override
	public UploadWebSmDTO toUpdate(String fileId) {
		return webSmRecDao.toUpdate(fileId);
	}

	@Override
	public void delAllByFileIds(List<String> list) {
		if (!list.isEmpty() && list.size() > 0) {
			webSmRecDao.delAllByFileIds(list);
		}
	}

	@Override
	public List<UploadWebSmDTO> selectRoot(Map<String, Object> params) {
		return webSmRecDao.selectRoot(params);
	}

	@Override
	public int countRoot(Map<String, Object> params) {
		return webSmRecDao.countRoot(params);
	}

	@Override
	public List<UploadWebSmDTO> selectUnit(Map<String, Object> params) {
		return webSmRecDao.selectUnit(params);
	}

	@Override
	public int countUnit(Map<String, Object> params) {
		return webSmRecDao.countUnit(params);
	}

	@Override
	public List<UploadWebSmDTO> getRecordRoot(Map<String, Object> params) {
		return webSmRecDao.getRecordRoot(params);
	}

	@Override
	public List<UploadWebSmDTO> getRecordUnit(Map<String, Object> params) {
		return webSmRecDao.getRecordUnit(params);
	}

	@Override
	public void save(WebSmRec entity) {
		entity.setSmSeq(this.getSmSeq(entity));
		entity.setSendTime(new Date());
		entity.setUserId(SysLoginUtil.getLoginUserId());
		webSmRecDao.insertSelective(entity);
	}
	
	/**
	 * 获取主键 序列
	 * @param entity WebSmRec
	 * @return smSeq
	 */
	private String getSmSeq(WebSmRec entity) {
		return entity.getSvrNode().substring(1) + DateTimeUtils.dateToStr(new Date(), "MMddHHmmss") + webSmRecDao.getSeqPart3();
	}

	@Override
	public WebSmRec findUserLast() {
		return webSmRecDao.findUserLast(SysLoginUtil.getLoginUserId());
	}

}
